package com.fick.hibernate.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.fick.hibernate.model.Message;

/**
 * A data access object (DAO) providing persistence and search support for
 * Message entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.fick.hibernate.model.Message
 * @author MyEclipse Persistence Tools
 */

public class MessageDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(MessageDAO.class);

	public List findAll() {
		log.debug("finding all Message instances");
		try {
			String queryString = "from Message";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Message findById(java.lang.Integer id) {
		log.debug("getting Message instance with id: " + id);
		try {
			Message instance = (Message) getSession().get("com.fick.hibernate.model.Message", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findPageByHql(String hql, Integer currPage, Integer pageSize) {
		log.debug("find page by hql" + hql);
		try {
			String queryString = "from Message " + hql;
			log.warn(queryString);
			Query query = getSession().createQuery(queryString);
			query.setFirstResult((currPage - 1) * pageSize);
			query.setMaxResults(pageSize);
			List li = query.list();
			return li;
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error("find page by hql is filse", e);
		}
		return null;
	}

}